﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XSTDZ.ERP.Application;
using XSTDZ.ERP.Infrastructure;
using XSTDZ.ERP.Application.DataContracts;
using XSTDZ.ERP.Infrastructure.Authentication;
using XSTDZ.ERP.WebApp.Models.Ado;

namespace XSTDZ.ERP.WebApp.Controllers
{
    //产品工序控制器
    public class ProductProcedureController : Controller
    {
        //
        // GET: /ProductProcedure/
        private readonly IProductService productProcedureServiceImpl = ServiceLocator.Instance.GetService<IProductService>();
        public ActionResult Index(Guid productId)
        {
            ViewBag.ProductId = productId;
            return View();
        }
        #region 提供选择产品工序(按条件）
         [NoAuthorizated("Select")]
        public ActionResult Select()
        {
            return View();
        }
        #endregion
        #region 添加
         [NoAuthorizated("Add")]
        public ActionResult Add(Guid productId)
        {
            var ppDto = new ProductProcedureDto { ProductID = productId };
            return View(ppDto);
        }
        [HttpPost]
        [NoAuthorizated("Add")]
        public ActionResult Add(ProductProcedureDto productProcedureDto, Guid productId)
        {
            if (ModelState.IsValid)
            {
                var id = productProcedureServiceImpl.AddProductProcedure(productProcedureDto);
                ViewBag.PostState = 9;
            }
            return View(productProcedureDto);
        }
        #endregion
        #region 修改
        [HttpGet]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(Guid productId)
        {
            var pp = productProcedureServiceImpl.GetProductProcedure(productId);
            return View(pp);
        }
        [HttpPost]
        [NoAuthorizated("Edit")]
        public ActionResult Edit(ProductProcedureDto productProcedureDto)
        {
            if (ModelState.IsValid)
            {
                productProcedureServiceImpl.UpdateProductProcedure(productProcedureDto);
                ViewBag.PostState = 9;
            }
            return View(productProcedureDto);
        }
        #endregion
        #region 删除
        [HttpGet]
        [NoAuthorizated("Delete")]
        public ActionResult Delete(Guid productId)
        {
            productProcedureServiceImpl.DeleteProductProcedure(productId);
            return Content("ok");
        }
        #endregion

        #region 获得产品工序列表
        //根据产品ID获得指定产品的工序
        [NoAuthorizated("GetJsonOfProductProcedure")]
        public ActionResult GetJsonOfProductProcedure(Guid productId)
        {
            var pplist = productProcedureServiceImpl.GetAllProductProcedure(productId);

            if (pplist != null && pplist.Count() != 0)
            {
                var jsonResult = pplist.Select(u => { u.DeletedState = u.Deleted == 0 ? "正常" : "已删除"; return u; });
                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"" + pplist.Count() + "\"}";
                return Content(target);
            }
            return Content("");
        }
         [NoAuthorizated("GetJsonOfProductProcedures")]
        public ActionResult GetJsonOfProductProcedures()
        {
            #region EF的查询，效率低下，故暂时放弃不用
            /*
            var pplist = productProcedureServiceImpl.GetAllProductProcedure(productId);

            if (pplist != null && pplist.Count() != 0)
            {
                var jsonResult = pplist.Select(u => { u.DeletedState = u.Deleted == 0 ? "正常" : "已删除"; return u; });
                var target = Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult);
                target = "{\"Rows\":" + target + ",\"Total\":\"" + pplist.Count() + "\"}";
                return Content(target);
            }
            return Content("");
              */
            #endregion
            return Content(new BLLQuery().GetProductProducts(this.HttpContext));
        } 
        #endregion
    }
}
